package org.dyk.seamanshousefrontdesk.controller;

import org.dyk.seamanshousefrontdesk.model.User;
import org.dyk.seamanshousefrontdesk.service.user.UserService;
import org.dyk.seamanshousefrontdesk.tools.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    /**
     * 登录 页面
     * @return
     */
    @RequestMapping(value = {"/login"})
    public String login(){
        return "login";
    }

    /**
     * 登录验证
     * @param loginName
     * @param userPassword
     * @param session
     * @return
     */
    @RequestMapping(value = {"/checkLogin"})
    @ResponseBody
    public boolean checkLogin(@RequestParam String loginName,
                              @RequestParam String userPassword,
                              HttpSession session) {
        User login = userService.login(loginName, userPassword);
        if (null != login){
            //验证通过，用户登录
            session.setAttribute(Constants.USER_SESSION,login);
            return true;
        }else {
            //验证不通过
            return false;
        }
    }

    /**
     * 注册 页面
     * @return
     */
    @RequestMapping(value = {"/register"})
    public String register(){
        return "register";
    }

    /**
     * 注册账户
     * @param loginName
     * @param userPassword
     * @return
     */
    @RequestMapping("/checkReqister")
    @ResponseBody
    public boolean checkReqister(@RequestParam String loginName, @RequestParam String userPassword, @RequestParam String userName){
        boolean register = userService.register(loginName, userPassword, userName);
        if (register){
            return true;
        }else {
            return false;
        }
    }
}
